import { SecTimer } from "../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../com/gengine/core/frame/TimerType";
import { Alert } from "../../../../com/mui/controls/Alert";
import { TimerAlertWin } from "../common/alertwins/TimerAlertWin";
import { Language } from "../../../../extend/language/Language";
import { DisplayUtil } from "../../../common/DisplayUtil";
import { Cache } from "../../cache/Cache";
import { ECopyType } from "../../../../Message/Public/ECopyType";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { RolePlayer } from "../../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../../scene/events/PlayerEvent";
type int = number;
//class AutoKickOutManager
    
    export  class AutoKickOutManager
    {
        constructor()
        {
            
            if (AutoKickOutManager._instance != null) 
            {
                throw new Error("AutoKickOutManager 单例");
            }
            return;
        }

        public start(): void
        {
            if (this._timer == null) 
            {
                this._timer = new SecTimer(this._delay);
                this._timer.addListener(TimerType.ENTERFRAME, this.onTimerHandler);
            }
            this._timer.reset();
            this._timer.start();
            this.addListener();
            return;
        }

        /* internal  */onTimerHandler(arg1: SecTimer): void
        {
            this._standCount = this._standCount + this._delay;
            if (this._standCount > 150) 
            {
                if (!this._alertSp) 
                {
                    Alert.timerNO = true;
                    Alert.alertWinRenderer = TimerAlertWin;
                    Alert.buttonWidth = 80;
                    Alert.timerOut = 30;
                    Alert.timerOutHandler = this.onAlertTimeOutHandler;
                    this._alertSp = Alert.show(Language.getString(90331), null, Alert.CANCEL, null, this.onCloseHandler);
                }
            }
            else if (this._alertSp) 
            {
                DisplayUtil.removeMe(this._alertSp);
                this._alertSp = null;
            }
            return;
        }

        /* internal  */onCloseHandler(arg1: int): void
        {
            this._standCount = 0;
            return;
        }

        /* internal  */onAlertTimeOutHandler(arg1): void
        {
            if (!Cache.instance.copy.isInCopy) 
            {
                return;
            }
            if (Cache.instance.copy.isInCopyType(ECopyType._ECopyNewBattle)) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.NewBattlefieldRequestLeaveAffirm));
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeaveWithoutComfirm));
            }
            return;
        }

        public stop(): void
        {
            if (this._timer) 
            {
                this._timer.stop();
                this._timer.dispose();
                this._timer = null;
            }
            this._standCount = 0;
            this.removeListener();
            return;
        }

        /* internal  */addListener(): void
        {
            RolePlayer.instance.addEventListener(PlayerEvent.ON_WALK, this.onPlayerOptHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.Enter_Fight, this.onPlayerOptHandler);
            return;
        }

        /* internal  */removeListener(): void
        {
            RolePlayer.instance.removeEventListener(PlayerEvent.ON_WALK, this.onPlayerOptHandler);
            RolePlayer.instance.removeEventListener(PlayerEvent.Enter_Fight, this.onPlayerOptHandler);
            return;
        }

        /* internal  */onPlayerOptHandler(arg1: Object /* flash.events.Event */): void
        {
            this._standCount = 0;
            return;
        }

        public static get instance(): AutoKickOutManager
        {
            if (AutoKickOutManager._instance == null) 
            {
                AutoKickOutManager._instance = new AutoKickOutManager();
            }
            return AutoKickOutManager._instance;
        }

        /* internal */ /* var */_timer: SecTimer;

        /* internal */ /* var */_delay: int=3;

        /* internal */ /* var */_standCount: int;

        /* internal */ /* var */_alertSp: Object /* flash.display.Sprite */;

        /* internal */ static /* var */_instance: AutoKickOutManager;
    }
